﻿///**************************************
/// Tevaganthan Veluppillai.     Sep 03 2009
/// Ultimate analysis page.
///

#region Namespaces
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
#endregion

public partial class Admin_UltimateAnalysis : System.Web.UI.Page
{
    Service objService = new Service();

    #region UI Events

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillUltimateAnalysisDetails(Convert.ToInt32(id));
                txtUltimateAnalId.Text = string.Empty;
                trDgUltimateAnalysis.Visible = false;
                trUltimateTextboxes.Visible = true;
            }

            fillDataGrid();
        }
    }
    private void ClearTextBoxes()
    {
        txtAsh.Text = string.Empty;
        txtC.Text = string.Empty;
        txtCH.Text = string.Empty;
        txtCl.Text = string.Empty;
        txtCO.Text = string.Empty;
        txtH.Text = string.Empty;
        txtMoisture.Text = string.Empty;
        txtN.Text = string.Empty;
        txtO.Text = string.Empty;
        txtS.Text = string.Empty;
        txtUltimateAnalId.Text = string.Empty;
        txtMoisture.Text = string.Empty;
    }

    private void fillUltimateAnalysisDetails(Int32 Id)
    {

        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("ultanal", Id);

        txtUltimateAnalId.Text = ds.Tables[0].Rows[0]["Id"].ToString();
        txtC.Text = ds.Tables[0].Rows[0]["C"].ToString();
        ddlMoistureStatus.SelectedValue = ds.Tables[0].Rows[0]["moistureStatus"].ToString();
        txtMoisture.Text = ds.Tables[0].Rows[0]["moisture"].ToString();
        txtAsh.Text = ds.Tables[0].Rows[0]["ash"].ToString();
        txtH.Text = ds.Tables[0].Rows[0]["H"].ToString();
        txtC.Text = ds.Tables[0].Rows[0]["C"].ToString();
        txtN.Text = ds.Tables[0].Rows[0]["N"].ToString();
        txtO.Text = ds.Tables[0].Rows[0]["O"].ToString();
        txtCl.Text = ds.Tables[0].Rows[0]["Cl"].ToString();
        txtS.Text = ds.Tables[0].Rows[0]["S"].ToString();
        txtCH.Text = ds.Tables[0].Rows[0]["H_C"].ToString();
        txtCO.Text = ds.Tables[0].Rows[0]["O_C"].ToString();
              
    }

    protected void dgUltimateAnal_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgUltimateAnalysis.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }

    protected void dgUltimateAnalysis_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgUltimateAnalysis.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillUltimateAnalysisDetails(id);
        trDgUltimateAnalysis.Visible = false;
        trUltimateTextboxes.Visible = true;
        btnPrevious.Visible = false;
    }

    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        string RefeId = (string)Session["ReferenceId"];

        if (txtUltimateAnalId.Text != string.Empty)
        {
            lblMessage.Text = objService.UpdateTable("ultanal", "moistureStatus", ddlMoistureStatus.SelectedValue.ToString(), txtUltimateAnalId.Text);
            if (txtMoisture.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "moisture", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "moisture", txtMoisture.Text, txtUltimateAnalId.Text);
            }

            if (txtAsh.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "ash", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "ash", txtAsh.Text, txtUltimateAnalId.Text);
            }

            if (txtH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "H", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "H", txtH.Text, txtUltimateAnalId.Text);
            }

            if (txtC.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "C", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "C", txtC.Text, txtUltimateAnalId.Text);
            }
            if (txtN.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "N", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "N", txtN.Text, txtUltimateAnalId.Text);
            }

            if (txtO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O", txtO.Text, txtUltimateAnalId.Text);
            }
            if (txtCl.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "Cl", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "Cl", txtCl.Text, txtUltimateAnalId.Text);
            }
            if (txtS.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "S", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "S", txtS.Text, txtUltimateAnalId.Text);
            }
            if (txtCH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "H_C", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "H_C", txtCH.Text, txtUltimateAnalId.Text);
            }

            if (txtCO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O_C", "null", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O_C", txtCO.Text, txtUltimateAnalId.Text);
            }

            if (txtO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O_calc", "1", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O_calc", "0", txtUltimateAnalId.Text);
            }

            if (txtCH.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "H_C_calc", "1", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "H_C_calc", "0", txtUltimateAnalId.Text);
            }

            if (txtCO.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O_C_calc", "1", txtUltimateAnalId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("ultanal", "O_C_calc", "0", txtUltimateAnalId.Text);
            }
            ClearTextBoxes();

            fillDataGrid();
            
        }
        else
        {
            #region UI Logic
            string O_calc = string.Empty;
            string H_C_calc = string.Empty;
            string O_C_calc = string.Empty;

            if (txtO.Text == string.Empty)
            {
                O_calc = "1";
            }
            else
            {
                O_calc = "0";
            }

            if (txtCH.Text == string.Empty)
            {
                H_C_calc = "1";
            }
            else
            {
                H_C_calc = "0";
            }
            if (txtCO.Text == string.Empty)
            {
                O_C_calc = "1";
            }
            else
            {
                O_C_calc = "0";
            }

            #endregion

            lblMessage.Text = objService.SaveUltAnalysis(ddlMoistureStatus.SelectedValue.ToString(), txtMoisture.Text, txtAsh.Text, txtH.Text, txtC.Text, txtN.Text, txtO.Text, O_calc, txtCl.Text, txtS.Text, txtCH.Text, H_C_calc, txtCO.Text, O_C_calc);

            string UltimateAnaId = objService.GetLastInsertedId("ref_fk", RefeId, "ultanal");

            objService.UpdateTable("ultanal", "ref_fk", RefeId, UltimateAnaId);

            ClearTextBoxes();
            
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];
                if (objService.UpdateExperiment("ultanal_fk", UltimateAnaId, ExpId) != "Experiment Successfully Updated")
                {
                    objService.DeleteRecord("ultanal", Convert.ToInt32(UltimateAnaId));
                    lblMessage.Text = "Admin - You can't enter the multiple Ultimate analysis records for one experiment";
                    fillDataGrid();
                }
                else
                {
                    lblMessage.Text = "Ultimate Analysis Saved and " + objService.UpdateExperiment("ultanal_fk", UltimateAnaId, ExpId);
                    fillDataGrid();
                }
                
                lblExpdetails.Text = "Your Experiment Id is - " + ExpId;
            }
            else
            {

                lblMessage.Text = "Ultimate Analysis Saved and " + objService.SaveExperiment(null, null, null, null, null, null, null, null, null, UltimateAnaId, null, RefeId,null);
                string ExperimentId = objService.GetExperimentId("ultanal_fk", UltimateAnaId);
                Session["ExperimentId"] = ExperimentId;
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
                fillDataGrid();

            }
        }

    }

    #endregion

    #region Private Methods

    private void fillDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId == null)
        {
            lblMessage.Text = "You haven't entered any records for Ultimate analysis";
            trUltimateTextboxes.Visible = true;
            trDgUltimateAnalysis.Visible = false;
        }
        else
        {
            Int32 UltAnalId = objService.GetExperimentFKValues("ultanal_fk", ExpId);
            if (UltAnalId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any records for Ultimate analysis";
                trUltimateTextboxes.Visible = true;
                trDgUltimateAnalysis.Visible = false;
            }
            else
            {
                if (objService.GetUltimateAnalysis(UltAnalId.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgUltimateAnalysis.DataSource = objService.GetUltimateAnalysis(UltAnalId.ToString());
                    dgUltimateAnalysis.DataBind();
                    trDgUltimateAnalysis.Visible = true;
                    trUltimateTextboxes.Visible = false;
                }
                else
                {
                    trDgUltimateAnalysis.Visible = false;
                    trUltimateTextboxes.Visible = true;
                }
            }
        }
    }

   

    #endregion       

    
   
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreUltimateAnalysis.aspx");
    }
}